package com.sxjh.service.impl;

import cn.hutool.extra.cglib.CglibUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sxjh.entity.VehicleType;
import com.sxjh.entity.param.VehicleTypeAddParam;
import com.sxjh.mapper.VehicleTypeMapper;
import com.sxjh.service.VehicleTypeService;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 车辆类型表  服务实现类
 * </p>
 *
 * @author lwj
 * @since 2022-07-20
 */
@Service
public class VehicleTypeServiceImpl extends ServiceImpl<VehicleTypeMapper, VehicleType> implements VehicleTypeService {

    @Override
    public boolean saveInfo(VehicleTypeAddParam param) {
        VehicleType entity = CglibUtil.copy(param, VehicleType.class);
        entity.setVersion(1);
        //处理值，保证唯一
        synchronized (this){
            int value = this.getMaxValueInfo();
            entity.setValue(value + 1);
        }
        return super.save(entity);
    }

    /**
     * 获取最大值
     * @return
     */
    public int getMaxValueInfo(){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.orderByDesc("value");
        queryWrapper.last("limit 1");
        VehicleType vehicleType = baseMapper.selectOne(queryWrapper);
        if(null == vehicleType){
            return 0;
        }
        return vehicleType.getValue();
    }
}
